package g2.routemaster.gui.contents;

import g2.routemaster.gui.GManager;
import g2.routemaster.gui.actions.ShowBonusesAction;
import g2.routemaster.gui.actions.ShowCardsAction;
import g2.routemaster.gui.general.ImageRegistry;
import g2.routemaster.model.CoachCard;
import g2.routemaster.model.Player;

import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;

public class PlayerGroup extends Composite {
   private Group group;
   private Label lHandCards;
   private Label lBoardCards;
   private Label lCoachCard;
   private Label lTotalBonus;
   private ShowCardsAction showCardsAction;
   private ShowBonusesAction showBonusesAction;   
   
   public PlayerGroup(Composite parent, int style) {
      super(parent, style);
   }

   public void create(){
      group = new Group(this, SWT.NONE);
      GridLayout layout = new GridLayout();
      layout.numColumns = 2;
      layout.makeColumnsEqualWidth = true;
      group.setLayout(layout);
      group.setLayoutData(new GridData(GridData.FILL_BOTH));
      group.setFont(getPlayerFont(group));
      
      ToolBar toolBar = new ToolBar(group, SWT.HORIZONTAL | SWT.FLAT);
      toolBar.setLayout(new GridLayout());
      GridData gd = new GridData();
      gd.horizontalSpan = 2;
      gd.grabExcessHorizontalSpace=true;
      toolBar.setLayoutData(gd);
      
      Label lCardsLabel = new Label(group, SWT.NONE);
      lCardsLabel.setText("# of Hand Cards:");
      lCardsLabel.setFont(getSimpleFont(lCardsLabel));
      
      lHandCards = new Label(group, SWT.NONE);
      lHandCards.setText("---");
      lHandCards.setFont(getSimpleFont(lHandCards));
      lHandCards.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      Label lBoardCardsLabel = new Label(group, SWT.NONE);
      lBoardCardsLabel.setText("# of Board Cards:");
      lBoardCardsLabel.setFont(getSimpleFont(lCardsLabel));
      
      lBoardCards = new Label(group, SWT.NONE);
      lBoardCards.setText("---");
      lBoardCards.setFont(getSimpleFont(lBoardCards));
      lBoardCards.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      Label lCoachCardLabel = new Label(group, SWT.NONE);
      lCoachCardLabel.setText("Coach Card:");
      lCoachCardLabel.setFont(getSimpleFont(lCoachCardLabel));

      lCoachCard = new Label(group, SWT.NONE);
      lCoachCard.setText("---");
      lCoachCard.setFont(getSimpleFont(lCoachCard));
      lCoachCard.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      Label lTotalBonusLabel = new Label(group, SWT.NONE);
      lTotalBonusLabel.setText("Total Points:");
      lTotalBonusLabel.setFont(getSimpleFont(lTotalBonusLabel));

      lTotalBonus = new Label(group, SWT.NONE);
      lTotalBonus.setText("---");
      lTotalBonus.setFont(getSimpleFont(lTotalBonus));
      lTotalBonus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      
      makeActions();
      makeToolBar(toolBar);
   }
   
   
   Font getPlayerFont(Control control){
      Font labelFont = control.getFont();
      FontData[] fontData = labelFont.getFontData();
      fontData[0].setHeight(10);
      fontData[0].setStyle(SWT.BOLD);
      return new Font(getDisplay(), fontData);
   }
   
   Font getSimpleFont(Label label){
      Font labelFont = label.getFont();
      FontData[] fontData = labelFont.getFontData();
      fontData[0].setHeight(8);
      fontData[0].setStyle(SWT.BOLD);
      return new Font(getDisplay(), fontData);
   }

   private void makeActions(){
      showCardsAction = new ShowCardsAction(GManager.instance().getWindow());
      showBonusesAction = new ShowBonusesAction(GManager.instance().getWindow());
   }
   
   private void makeToolBar(final ToolBar toolBar){
      ToolBarManager manager = new ToolBarManager(toolBar);
      manager.add(showCardsAction);
      manager.add(showBonusesAction);
      manager.update(true);
   }
   
   public void updateData(Player player){
      if(player.getTurnManager() != null){
         this.setBackground(new Color(Display.getCurrent(), ImageRegistry.getPlayerRGB(player.getColor().getName())));
      } else {
         this.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
      }
      String playerName = player.getName();
      int noCardsHand = player.getHand().sizeOfCityCard();
      int noCardsBoard = player.getPlayedStack().sizeOfCityCard();
      int freeBranches = (int)player.countOfFreeBranches();
      int score = player.getScore() + freeBranches;
      CoachCard coachCard = player.getHighestCoachCard();
      
      group.setText(playerName);
      lHandCards.setText(String.valueOf(noCardsHand));
      lBoardCards.setText(String.valueOf(noCardsBoard));
      if(coachCard == null){
         lCoachCard.setText("<none>");
      } else {
         lCoachCard.setText(coachCard.getValue() + " (length " + coachCard.getLength() + ")");
      }
      lTotalBonus.setText(score + " (" + freeBranches + " flags)");
      showCardsAction.setPlayer(player);
      showBonusesAction.setPlayer(player);
   }
   
   
}
